package app.controller;

import app.behavior.LogBehavior;
import extend.db.DbManager;
import extend.db.base.SqlMap;
import hehe.He;
import hehe.User;
import hehe.aop.Aop;
import hehe.aop.AopManager;
import hehe.aop.annotation.After;
import hehe.aop.annotation.Aspect;
import hehe.aop.annotation.Before;
import hehe.aop.annotation.Finally;
import hehe.container.build.annotation.Bean;
import hehe.log.Log;
import hehe.router.annotation.UrlMap;
import hehe.validation.annotation.Required;
import hehe.validation.annotation.Validator;
import hehe.web.WebController;
import hehe.worker.base.Pool;

import java.util.Map;

@Bean
@UrlMap(path = "/index")

public class IndexController extends WebController
{
    public int age = 10;
//@Length(number = 10,character = "gt",tip = "长度必须大于{number}")
    @Required(tip = "xx！")
    @Validator(name = "required",tip = "kfbbbbbff！",onEmpty = true)
    public String name;


    public String indexAction()
    {
//        LoggerPropertie loggerPropertie = new LoggerPropertie();
//        loggerPropertie.getAttrs("log.logger.easy");
//        Address Address = He.getBean(Address.class);
//
//        System.out.println(Address.find());

//        //Map<String,Object> attrs = ReflectionHelper.getAttrs(this,"age");
//        Map<String,Object> attrs = new HashMap<>();
//        attrs.put("age",20);
//
//        ReflectionHelper.setAttrs(this,attrs);
//        List<String> fields = ReflectionHelper.getFieldsName(this, Modifier.PUBLIC);


//        DbManager db = He.getBean("db");
//
//        User user = new User();
//        SqlMap where = SqlMap.mSet("UserId",110);
//        Map<String, Object> row = user
//                .field("*")
//                .where(where)
//                .one();
//
//        return "action run " + Math.random()
//                + "<br/>"
//                + ",result:"
//                +  row.toString()
//                + "<br/>"
//                + "sql" + db.getLastSql()
//               // + "attrs:" + attrs.toString()
//                + "age:"+this.age
//                //+ "fields:"+ String.join(",",fields)
//                + "<br/>";

        return ";";
    }

    public String addAction()
    {
        try {

            for (int i =0;i< 10000;i++) {
                Log.error("控制器的类型:" +  Math.random());
            }

            Thread.sleep(150);

            for (int i =0;i< 10000;i++) {
                Log.error("控制器的类型:" +  Math.random());
            }

            System.out.println("到这里了" + Math.random());
        } catch (Exception ex) {
            ex.printStackTrace();
        }


//        Validation validation = new Validation();
//
////        validation.on("name").add(new Verify("required").tip("xxx"))
////                .on("age",new Verify("required")).tip("消息okd");
////        ;
//        if (validation.validate(this)) {
//            System.out.println("验证通过" + Math.random());
//        } else {
//            System.out.println("验证未通过" + Math.random() + ",消息:" + validation.getFirstError());
//        }

//        RequiredValidator requiredValidator = new RequiredValidator();
//        String name = "";
//        System.out.println("name:" + requiredValidator.validate(name));

//        System.out.println("action run " + Math.random());
//
//        Url url = He.getBean("router");
//
//        String uri = url.createUrl("index/user",null,null);
//
//        return "url:" + uri +  ",action run " + Math.random();
        return "";
    }

    @Before(behavior = {LogBehavior.class})
    @After(behavior = {LogBehavior.class})
    @Finally(behavior = {LogBehavior.class})
    @Aspect(advice = Aop.BEFORE_EVENT,behavior = {LogBehavior.class})
    public void ok()
    {
        System.out.println("代理了...");
    }

    public void worker()
    {
        long workerTaskPooltime = System.currentTimeMillis();
        Pool workerTaskPool = He.getBean("workers");

        for (int i = 0;i < 1000000;i++) {
            workerTaskPool.pull(new Runnable(){
                public void  run()
                {
                    System.out.println("耗时:" + (System.currentTimeMillis() - workerTaskPooltime) + " ms");
                };
            });
        }
    }
}
